//
//  AppDelegate.m
//  ShopingAssistant
//
//  Created by zy on 16/10/9.
//  Copyright © 2016年 zy. All rights reserved.
//

#import "AppDelegate.h"
#import <AlipaySDK/AlipaySDK.h>
#import "NNbHtmlWebViewController.h"
#import "LaunchViewController/LunchViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface AppDelegate ()

@end

//wheatHongGaoLiang
@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [UIViewController new];
    
    [self launchContro];
    
    [WXApi registerApp:@"wx332ba913d7ae5904"];//注册appid
    return YES;
}

-(void)launchContro{
    BOOL isFirst = [LunchViewController canShowNewFeature];
//        BOOL isFirst = 1;
    if (isFirst) {
        
        self.window.rootViewController =[LunchViewController newLunchVCWithModels:@[@"guid"] enterBlock:^{
             [self enter];
        }];
    }
    else {
       
        [self enter];
    }
    
    
}

-(void)enter{
    
    
    NNbHtmlWebViewController *MyWebVC=[NNbHtmlWebViewController new];
    UINavigationController *navRoot=[[UINavigationController alloc]initWithRootViewController:MyWebVC];
    self.window.rootViewController=navRoot;
    
    
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    if ([[url scheme] isEqualToString:@"wx332ba913d7ae5904"])
    {
        BOOL wexin = [WXApi handleOpenURL:url delegate:self];
        NSString *valueresult = @"0";
        if (wexin) {//成功
            valueresult = @"1";
        }
        NSDictionary *dict = [NSDictionary dictionaryWithObject:valueresult forKey:@"resultStatus"];
        
        // 2.创建通知
        
        NSNotification *notification =[NSNotification notificationWithName:@"InfoNotification" object:nil userInfo:dict];
        
        // 3.通过 通知中心 发送 通知
        
        [[NSNotificationCenter defaultCenter] postNotification:notification];
//        return wexin;
        
    }else if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            NSString *valueresult = @"1";
            if ([resultDic[@"resultStatus"] integerValue] == 9000) {
                valueresult = @"0";
            }
            NSDictionary *dict = [NSDictionary dictionaryWithObject:valueresult forKey:@"resultStatus"];
            
            // 2.创建通知
            
            NSNotification *notification =[NSNotification notificationWithName:@"InfoNotification" object:nil userInfo:dict];
            
            // 3.通过 通知中心 发送 通知
            
            [[NSNotificationCenter defaultCenter] postNotification:notification];
        }];
    }
    return YES;
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([[url scheme] isEqualToString:@"wx332ba913d7ae5904"])
    {
        BOOL wexin = [WXApi handleOpenURL:url delegate:self];
        NSString *valueresult = @"0";
        if (wexin) {//成功
            valueresult = @"1";
        }
        NSDictionary *dict = [NSDictionary dictionaryWithObject:valueresult forKey:@"resultStatus"];
        
        // 2.创建通知
        
        NSNotification *notification =[NSNotification notificationWithName:@"InfoNotification" object:nil userInfo:dict];
        
        // 3.通过 通知中心 发送 通知
        
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
//        return wexin;
    }else if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            NSString *valueresult = @"0";
            if ([resultDic[@"resultStatus"] integerValue] == 9000) {
                valueresult = @"1";
            }
            NSDictionary *dict = [NSDictionary dictionaryWithObject:valueresult forKey:@"resultStatus"];
            
            // 2.创建通知
            
            NSNotification *notification =[NSNotification notificationWithName:@"InfoNotification" object:nil userInfo:dict];
            
            // 3.通过 通知中心 发送 通知
            
            [[NSNotificationCenter defaultCenter] postNotification:notification];
        }];
    }
    return YES;
}



//-(void)onResp:(BaseResp *)resp{
//    if ([resp isKindOfClass:[PayResp class]]){
//        PayResp *response=(PayResp *)resp;
//
//        NSString *valueresult = @"1";
//        if (response.errCode  == 0) { //0    成功    展示成功页面
//            valueresult = @"0";
//        }
//        NSDictionary *dict = [NSDictionary dictionaryWithObject:valueresult forKey:@"resultStatus"];
//
//        // 2.创建通知
//
//        NSNotification *notification =[NSNotification notificationWithName:@"InfoNotification" object:nil userInfo:dict];
//
//        // 3.通过 通知中心 发送 通知
//
//        [[NSNotificationCenter defaultCenter] postNotification:notification];
//    }
//}

@end
